package org.ejb.entities;



import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Cette classe represente un utilisateur
 */

@SuppressWarnings("serial")
@Entity
public class Utilisateur implements Serializable {

    /**
	 * 
	 */
	// ======================================
    // =             Attributs              = 
    // ======================================
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    /** Prenom */
	private String firstname;
      
	/** Nom de famille */
	private String lastname;
	
	/** Sexe */
	private String gender;
	
	/** Mot de passe*/
	private String password;
 
	/** L'adresse e-mail du client.*/
	private String email;

//	private static final String MALE = "Male";
//	private static final String FEMALE = "Female";
	
	/** Solde du compte client */
	private Integer balance;

	/** Adresse de facturation de client */
	@OneToOne(cascade=CascadeType.ALL)
	private Address address;
	
	/** Numero de telephone du client */
	private String telephone;
	
	/** Annonce que peut eventuellement poster un client */
	@OneToMany(mappedBy="utilisateur")
	private List<Annonce> annonce = new ArrayList<Annonce>();
	
	@OneToMany(mappedBy="utilisateur")
	private List<OrderParFois> orderParFois = new ArrayList<OrderParFois>();
	
//	/** Location - les infos pour les prets */
//	@OneToMany(mappedBy="utilisateur")
//	private List<Location> location = new ArrayList<Location>();
	
	/** Date de naissance */
//	@Temporal(TemporalType.DATE)
//	private Date dateOfBirth;
	
	/** Abonnement d'un client */
    @ManyToOne(cascade=CascadeType.REFRESH)
	private Abonnement abonnement;
	
	/** Age du client */
	@Transient
	private Integer age;
    
    // ======================================
    // =            Constructeurs           =
    // ======================================
    public Utilisateur() {
    }

    public Utilisateur(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
    
    public Utilisateur(String firstname, String lastname, String email , String password, String gender, String telephone, Integer balance) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.telephone = telephone;
        this.password = password;
        this.gender = gender;
        this.email = email;
        this.balance = balance;
    }
    
    public Utilisateur(String firstname, String lastname, String password, String telephone, String gender, String email, Integer balance, Integer age) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.telephone = telephone;
        this.password = password;
        this.gender = gender;
        this.email = email;
        this.balance = balance;
        this.age = age;
    }
    // ======================================
    // =     Methodes Lifecycle Callback    =
    // ======================================
//    @SuppressWarnings("unused")
//	@PrePersist
//	@PreUpdate
//	private void validateData() {)
//		if (firstname == null || "".equals(firstname))
//			throw new ValidationException("Prenom invalide");
//		if (lastname == null || "".equals(lastname))
//			throw new ValidationException("Nom invalide");
//		if (email == null || "".equals(email))
//			throw new ValidationException("login (e-mail) invalide");
//		if (password == null || "".equals(password))
//			throw new ValidationException("Mot de passe invalide");
//		if (gender == null || "".equals(password) || gender.equals("null"))
//			throw new ValidationException("Sexe invalide");
//	}

    /** Cette methode calcul l'age du client */
//    @PostLoad
//    @PostPersist
//    @PostUpdate
//    public void calculateAge() {
//        if (dateOfBirth == null) {
//            age = null;
//            return;
//        }
//
//        Calendar birth = new GregorianCalendar();
//        birth.setTime(dateOfBirth);
//        Calendar now = new GregorianCalendar();
//        now.setTime(new Date());
//        int adjust = 0;
//        if (now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) < 0) {
//            adjust = -1;
//        }
//        age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust;
//    }

    // ======================================
    // =          Methodes publiques        =
    // ======================================
//	@Column(nullable = false)

    /**
     * Given a password, this method then checks if it matches the user
     *
     * @param pwd
     * @throws ValidationException thrown if the password is empty or different than the one
     *                             store in database
     */
//    public void matchPassword(String pwd) {
//        if (pwd == null || "".equals(pwd))
//            throw new Validation	@OneToOneException("Invalid password");
//
//        // The password entered by the customer is not the same stored in database
//        if (!pwd.equals(password))
//            throw new ValidationException("Passwords don't match");
//    }

    // ======================================
    // =             Accesseurs             = 
    // ======================================

    /** @return the id */
	@Column(nullable = false)
	public long getId() {
		return id;//	@Column(nullable = false)
	}
	public void setId(long id) {
		this.id = id;
	}
	
	/** @return the firstName */
	@Column(nullable = false)
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstName) {
		this.firstname = firstName;
	}
	
	/** @return the lastName */
	@Column(nullable = false)
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastName) {
		this.lastname = lastName;
	}

	/** @return the email */
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	/** @return the password */
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	/** @return the gender */
	/* faire un combobox pour recuperer la valeur dont soit male soit female *****************************************************/ 
	public String getGender() {
		return gender;
//		if (gender== null) return "Inconnu";
//		if (gender.equals(MALE)) return "Homme";
//		else if (gender.equals(FEMALE)) return "Femme";
//		return "Inconnu";
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	/** @return the annonce */
	public List<Annonce> getAnnonce() {
		return annonce;
	}
	public void setAnnonce(List<Annonce> annonce) {
		this.annonce = annonce;
	}
	
//	/** @return the Location */
//	public List<Location> getLocation() {
//		return location;
//	}
//	public void setLocation(List<Location> location) {
//		this.location = location;
//	}
	
	/** @return order */
	public List<OrderParFois> getOrder() {
		return orderParFois;
	}
	public void setOrder(List<OrderParFois> orderParFois) {
		this.orderParFois = orderParFois;
	}
	
	/** @return the dateOfBirth */
//	public Date getDateOfBirth() {
//		return dateOfBirth;
//	}
//
//	/**	/** @param gender the gender to set */

//	 * @param dateOfBirth the dateOfBirth to set
//	 */
//	public void setDateOfBirth(Date dateOfBirth) {
//		this.dateOfBirth = dateOfBirth;
//	}

	/** @return the phoneNumber */
	public String getTelephone() {
		return telephone;
	}
	/** @param phoneNumber the phoneNumber to set */
	public void setTelephone(String phoneNumber) {
		this.telephone = phoneNumber;
	}

	/** @return the homeAddress */
	public Address getAddress() {
		return address;
	}
	/** @paramstudent homeAddress the homeAddress to set */
	public void setAddress(Address address) {
		this.address = address;
	}	

	public Integer getBalance() {
		return balance;
	}
	public void setBalance(Integer balance) {
		this.balance = balance;
	}
	
	public Abonnement getAbonnement() {
		return abonnement;
	}
	public void setAbonnement(Abonnement abonnement) {
		this.abonnement = abonnement;
		this.abonnement.getUtilisateur().add(this);
	}
	
	/** @return the age */
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}

    // ======================================
    // =   Methodes hash, equals, toString  =
    // ======================================
//	public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//
//        Utilisateur customer = (Utilisateur) o;
//
//        if (dateOfBirth != null ? !dateOfBirth.equals(customer.dateOfBirth) : customer.dateOfBirth != null)
//            return false;
//        if (email != null ? !email.equals(customer.email) : customer.email != null) return false;
//        if (!firstname.equals(customer.firstname)) return false;
//        if (!uid.equals(customer.uid)) return false;
//        if (!lastname.equals(customer.lastname)) return false;
//        if (!gender.equals(customer.gender)) return false;
//        if (!email.equals(customer.email)) return false;
//        if (!password.equals(customer.password)) return false;
//        if (telephone != null ? !telephone.equals(customer.telephone) : customer.telephone != null) return false;
//
//        return true;
//    }
//
//    @Override
//    public int hashCode() {
//        int result;
//        result = uid.hashCode();
//        result = 31 * result + password.hashCode();
//        result = 31 * result + firstname.hashCode();
//        result = 31 * result + lastname.hashCode();
//        result = 31 * result + gender.hashCode();
//        result = 31 * result + (telephone != null ? telephone.hashCode() : 0);
//        result = 31 * result + (email != null ? email.hashCode() : 0);
//        result = 31 * result + (dateOfBirth != null ? dateOfBirth.hashCode() : 0);
//        return result;
//    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Utilisateur");
        sb.append("{id=").append(id);
        sb.append(", password='").append(password).append('\'');
        sb.append(", firstname='").append(firstname).append('\'');
        sb.append(", lastname='").append(lastname).append('\'');
        sb.append(", telephone='").append(telephone).append('\'');
        sb.append(", gender='").append(gender).append('\'');
        sb.append(", email='").append(email).append('\'');
        sb.append(", homeAddress=").append(address);
        //sb.append(", dateOfBirth=").append(dateOfBirth);
        sb.append(", age=").append(age);
        sb.append('}');
        return sb.toString();
    }

}

